package com.eliansun.crawler.pageparse.jiangxi;

import com.eliansun.crawler.pageparse.AbstractPageProcess;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * user: lxc
 * data:2019- 07-03-09
 * 健康医药网:www.jk6999.com
 */
public class Jiankangyiyao extends AbstractPageProcess {

    int count = 0;

    @Override
    public void process(Page page) {
        //列表界面
        if(page.getUrl().toString().indexOf("search") != -1){

            List<Selectable> nodes = page.getHtml().xpath("//div[@class=Product]/ul/li/a").nodes();
            if(nodes.size() == 0){
                return;
            }
            //列表界面解析出对应的详情url，放到队列中
            for (Selectable node : nodes) {
                String s = node.links().toString();
                page.addTargetRequest( s );
            }

            //获取下一页放到队列中
            Object getTotalPage = page.getHtml().xpath("//i[@class=goods_num]/text()");
            if( getTotalPage != null ){
                //总页数
                Integer totalPage =  ( Integer.parseInt( getTotalPage.toString() )-1 )/20+1 ;
                for (int i = 1; i <totalPage; i++) {
                    String url = page.getUrl().toString();
                    page.addTargetRequest( url.replace("page=1","page="+ (i+1) ) );
                }
            }
        }else if(page.getUrl().toString().indexOf("detail") != -1){
            //详情界面
            Map<String, Object> resMap = new HashMap<>();
            // 商品名称
            resMap.put("goodsName",page.getHtml().xpath("//h2/text()").get());
            //价格
            String price =    page.getHtml().xpath("/html/body/div[3]/div[2]/div[2]/dl[2]/dd/b/text()").get();
            resMap.put("price", price.replace("￥",""));

            List<String> titleList = page.getHtml().xpath("//div[@class=specification_f]//span//b//text()").all();
            List<String> detailList = page.getHtml().xpath("//div[@class=specification_f]//dl//dd//text()").all();
            String medicalDeviceNo = "";
            String productCompany = "";
            String spec = "";
            for (int i = 0; i < titleList.size(); i++) {
                if(titleList.get(i).contains("批准文号")){
                    medicalDeviceNo = detailList.get(i).trim();
                }
                if(titleList.get(i).contains("生产企业")){
                    productCompany = detailList.get(i).trim();
                }
                if(titleList.get(i).contains("规格")){
                    spec = detailList.get(i).trim();
                }
            }

            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("spec", spec);
            resultMap.put("medicalDeviceNo", medicalDeviceNo);
            resultMap.put("productCompany", productCompany);
            resultMap.put("price",resMap.get("price"));
            resultMap.put("brand","");
            resultMap.put("productName",resMap.get("goodsName"));
            resultMap.put("executiveStandard","");
            resultMap.put("nationality","");
            resultMap.put("store",super.cTerrace);
            resultMap.put("url",page.getUrl());
            resultMap.put("version","");
            page.putField("result", resultMap);

            count ++;
            System.out.println(count );
        }
    }

    Site site = Site.me()
            //设置编码
            .setCharset("utf-8")
            //设置抓取间隔时间
            .setSleepTime(1000*3)
            //设置超时时间，单位毫秒
            .setTimeOut(1000*3)
            //设置重试间隔时间
            .setRetrySleepTime(1000*3)
            //设置重试次数
            .setRetryTimes(3);
    @Override
    public Site getSite() {
        return site;
    }



    public static void main(String[] args) {
        Spider.create( new Jiankangyiyao())
//                .addUrl("http://www.jk6999.com/product/detail?id=81")
                .addUrl("http://www.jk6999.com/search?keyword=%E9%92%99%E5%B0%94%E5%A5%87&cat=&sort=1&page=1")
                //设置多个线程同时抓取数据
                .thread(1)
                .run();
    }


}
